#include "SipTranB2bSession.hpp"

#include "SipTranSession.hpp"

namespace wtsip{

CSipTranB2bSession::CSipTranB2bSession(
    CSipGDService& srv, 
    CSipTranSession& caller, 
    CSipTranSession& callee): 
CSipB2BSession(srv, static_cast<CSipSessionBase&>(caller), static_cast<CSipSessionBase&>(callee))
{}

CSipTranB2bSession::~CSipTranB2bSession(){}


bool CSipTranB2bSession::startCaller(SipMessage& calleeReq)
{
    std::string body;
    if (calleeReq.getContents()){
        body = calleeReq.getContents()->getBodyData().c_str();
    }
    return m_caller.start(body);
}


///////////////////   handle method /////////////////////

bool CSipTranB2bSession::onSuccess(SipMessage& msg, CSipSessionBase& sb)
{
    if (&sb == &m_caller)
    {
        std::string body;
        if (msg.getContents()){
            body = msg.getContents()->getBodyData().c_str();
        }
        CSipTranSession* callee = dynamic_cast<CSipTranSession*>(&m_callee);
        if (callee){
            callee->replyOK(body);
        }
    }
    return true;
}


bool CSipTranB2bSession::onFailed(SipMessage& msg, CSipSessionBase& sb)
{
    if (&sb == &m_caller)
    {
        int code = msg.header(h_StatusLine).responseCode();
        std::string body;
        if (msg.getContents()){
            body = msg.getContents()->getBodyData().c_str();
        }
        CSipTranSession* callee = dynamic_cast<CSipTranSession*>(&m_callee);
        if (callee)
        {
            callee->reply(body, CSipSessionBase::getReasonbyCode(code));
        }
    }
    return true;
}


bool CSipTranB2bSession::onTrying(SipMessage& msg, CSipSessionBase& sb)
{
    if (&sb == &m_caller)
    {
        CSipTranSession* callee = dynamic_cast<CSipTranSession*>(&m_callee);
        if (callee){
            callee->replyTrying();
        }
    }
    return true;
}


bool CSipTranB2bSession::onDestroy(CSipSessionBase& sb)
{
    return destroy();
}


}//namespace
